package com.beeway.blackgoose.fragment;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.beeway.blackgoose.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;


/**
 * A simple {@link Fragment} subclass.
 */
public class BuyFragment extends Fragment implements  CompoundButton.OnCheckedChangeListener {
    @ViewInject(R.id.rg_buy_rgs)
    private RadioGroup rgs;
   @ViewInject(R.id.fl_buy_contanrs)
    private FrameLayout contanrsFl;

    private RadioButton[] rbs;
    private View view;


    public BuyFragment() {
    }

    public static BuyFragment getInstance() {
        BuyFragment buy = new BuyFragment();
        return buy;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view  = inflater.inflate(R.layout.fragment_buy, container, false);
        ViewUtils.inject(this,view);
        initView();
        return view;
    }

    private void initView() {
        getInitFragment();
        getRadioButton();
    }
    public void getRadioButton() {
        rbs = new RadioButton[rgs.getChildCount()];
        for (int i = 0; i < rbs.length ; i++) {
           rbs[i] = (RadioButton) rgs.getChildAt(i);
            rbs[i].setOnCheckedChangeListener(this);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (rbs[0].isChecked()){
            getFragments(PublishFragment.getInstance());
        }else if (rbs[1].isChecked()){
            getFragments(ListsFragment.getInstance());
        }
    }

    public void getFragments(Fragment ss) {
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction =manager.beginTransaction();
        transaction.replace(R.id.fl_buy_contanrs,ss);
        transaction.commit();
    }
    //默认的fragment
    public void getInitFragment() {
        PublishFragment publish = new PublishFragment();
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction =manager.beginTransaction();
        transaction.add(R.id.fl_buy_contanrs,publish);
        transaction.commit();
    }
}
